import 'package:app/common/app_theme.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/pages/personnel_module/department_staff_view.dart';
import 'package:app/pages/personnel_module/personnel_controller.dart';
import 'package:app/services/address.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:getwidget/components/checkbox/gf_checkbox.dart';
import 'package:getwidget/size/gf_size.dart';
import 'package:getwidget/types/gf_checkbox_type.dart';

import '../../common/colors.dart';

class PersonnelView extends GetView{


  PersonnelController controller = Get.put(PersonnelController());

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar:
      // BaseAppBar(
      //   preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
      //   child: Text('选择对象'),
      // ),
      PreferredSize(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: AppBar(
          backgroundColor: AppColor.themeColor,
          leading: IconButton(
            onPressed: () {
              controller.personnelNameArr.clear();
              controller.personnelIdArr.clear();
              for(int i = 0;i<controller.model!.data!.length;i++){
                controller.model?.data?[i].isSelection = false;
              }
              Get.back();
            },
            icon: Image.asset(
              'images/leftarr.png',
              width: 15,
              height: 15,
              color: Colors.white,
            ),
          ),
          title: Text('选择对象'),
        ),
      ),
      body: GetBuilder<PersonnelController>(builder: (_){
        return SafeArea(child: Column(
          children: [
            Container(
                decoration: BoxDecoration(
                    color: Colors.grey.shade200,
                    borderRadius: BorderRadius.all(Radius.circular(15))),
                width: Get.width - 50,
                alignment: Alignment.centerLeft,
                margin: EdgeInsets.only(top:15, left: 15, right: 15),
                padding: EdgeInsets.only(left: 15),
                height: 35,
                child: Row(
                  children: [
                    Icon(
                      Icons.search,
                      size: 20,
                      color: AppColor.smallTextColor,
                    ),
                    Container(
                      alignment: Alignment.center,
                      color: Colors.transparent,
                      width: Get.width - 170,
                      height: 30,
                      child: TextField(
                        controller: controller.editingController,
                        onChanged: (values) {
                          controller.requestDataWithHrEmployee(values);
                        },
                        decoration: InputDecoration(
                            isCollapsed: true,
                            border: InputBorder.none,
                            hintText: '请输入关键词',
                            hintStyle: TextStyle(
                                color: AppColor.hintColor, fontSize: 13)),
                        style: TextStyle(
                            color: AppColor.smallTextColor, fontSize: 14),
                      ),
                    )
                  ],
                )),

            Expanded(child: EasyRefresh.custom(
              slivers: [

                // SliverList(delegate: _departmentSliverChildBuilderDelegate(),),

                SliverList(delegate: _mySliverChildBuilderDelegate(),),
              ],
            ),),
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  MaterialButton(onPressed: (){},child: Text('已选(${controller.personnelIdArr.length})',style: appThemeData.textTheme.subtitle1!.copyWith(
                      fontSize: 15
                  ),),),
                  Row(
                    children: [
                      MaterialButton(onPressed: (){
                        controller.personnelNameArr.clear();
                        controller.personnelIdArr.clear();
                        for(int i = 0;i<controller.model!.data!.length;i++){
                          controller.model?.data?[i].isSelection = false;
                        }
                        Get.back();
                      },child: Text('取消',style: appThemeData.textTheme.bodyText1,),),
                      MaterialButton(onPressed: (){
                        controller.tapClickEnter();
                      },child: Text('确定',style: appThemeData.textTheme.bodyText1,),),

                    ],
                  )
                ],
              ),
              height: 50,
            )
          ],
        ));
      }),
    );
  }

  _departmentSliverChildBuilderDelegate(){
    return SliverChildBuilderDelegate(
            (BuildContext context, int index) {
          return GestureDetector(
            onTap: (){
              Get.to(DepartmentStaffView(department_id:controller.departmentModel?.data?[index].id,));
            },
            child: Container(
              color: Colors.transparent,
              child:Column(
                children: [
                  Container(
                    padding: EdgeInsets.all(10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            CircleAvatar(
                              backgroundImage: NetworkImage(
                                '${Address.homeHost}${controller.departmentModel?.data?[index].image128}',
                              ),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text('${controller.departmentModel?.data?[index].name}',style: appThemeData.textTheme.bodyText1,),
                              ],
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Container(
                    color: AppColor.lineColor,
                    height: 1,
                  )
                ],
              ),
            ),
          );
        },
        childCount: controller.departmentModel?.data?.length??0
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
        return Container(

          child:Column(
            children: [
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                   children: [
                     CircleAvatar(
                       backgroundImage: NetworkImage(
                         '${Address.homeHost}${controller.model?.data?[index].image128}',
                       ),
                       radius: 25,
                     ),
                     SizedBox(width: 5,),
                     Column(
                       crossAxisAlignment: CrossAxisAlignment.start,
                       children: [
                         Text('${controller.model?.data?[index].name}',style: appThemeData.textTheme.bodyText1,),
                         SizedBox(height: 5,),
                         Text('${controller.model?.data?[index].department}',style: appThemeData.textTheme.subtitle1,)
                       ],
                     ),
                   ],
                 ),
                  GFCheckbox(
                    onChanged: (bool value) {
                      controller.tapClickCheck(index);
                    },
                    size: 30,
                    type: GFCheckboxType.circle,
                    value:  controller.model!.data![index].isSelection!,
                  )
                ],
              ),
            ),
              Container(
                color: AppColor.lineColor,
                height: 1,
              )
            ],
          ),
        );
      },
      childCount: controller.model?.data?.length??0
    );
  }

}